define(function (require, exports, module) {
	var EditGrid = require("editgrid");
	var store =require("store");
	var activeUser = store.get("user");
	var Dialog = require("dialog");
	
    var instance = {
        initComponent: function (params, widget) {
            var editgrid = new EditGrid({
        	    parentNode: ".J-editgrid",
				model:{
					id:"editgrid",
					head:{
						title:"选择房间"
					},
					onAdd: function(editors) {
						var roomType = editors["roomType.name"];
						var roomCount = editors["roomCount"];
						if(roomType.getValue() == "" || roomCount.getValue() == ""){
							return false;
						}else{
							var str = /^[1-9]*[1-9][0-9]*$/;
							if(isNaN(roomCount.getValue()) || !str.test(parseFloat(roomCount.getValue()))){
                    			Dialog.alert({
                    				tip : "提示",
                    				content : "数量只能输入正整数！"
                    			})
								return false;
							}
							editgrid.add({
								roomType : roomType.getData(roomType.getValue()),
								roomCount : roomCount.getValue(),
								roomStatus : "Pending",
								version : "0"
							});
						}
			        },
					columns:[{
						name:"roomType.name",
						label:"房型",
						editor:{
							type:"select",
							url:"api/roomtype/query",
							params:function(){
								var obj={
		    	     				fetchProperties:"pkRoomType,name"
		                		}
		                		return obj;
							},
							keyField:"pkRoomType",
							valueField:"name",
							onEdit : function(editor,rowIndex,rowData){
								editor.setValue(rowData.roomType.pkRoomType);
							},
							onChange : function(plugin,index,rowData){
								rowData.roomtype = plugin.getData(plugin.getValue());
								editgrid.update(index,rowData);
							}
						}
					},{
                        name : "roomCount",
                        label : "数量",
                        editor: {
                            type: "text",
                            onChange: function(editor, rowIndex, rowData) {
                            	var str = /^[1-9]*[1-9][0-9]*$/;
                            	var roomCount = parseFloat(editor.getValue());
                        		if(isNaN(editor.getValue()) || !str.test(roomCount)){
                        			Dialog.alert({
                        				tip : "提示",
                        				content : "数量只能输入正整数！"
                        			})
                        		}else{
                        			rowData.roomCount = editor.getValue();
                        			editgrid.update(rowIndex, rowData);
                        		}
                            }
                        }
                    },{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"del",
							icon:"icon-remove",
							handler:function(index,data,rowEle){
								editgrid.remove(rowEle);
  							}
						}]
					}]
				}
			})
        	widget.set("editgrid", editgrid);
        }
    };
    module.exports = instance;
});
